import { ref, onMounted, inject } from "vue";
import { getProductModelDetail } from "@/api/modules/carType";
import { useDict } from "@/hooks/useDict";

const mainItemTitleList: any = {
  specifiedLoad: "额定负载(kg)",
  specifiedLoadDistance: "额定载荷中心距（mm）",
  loadHeight: "举升高度（mm",
  radarHeight: "标配雷达高度（mm）",
  standSizeCl: "标准货叉尺寸l/s/e（mm）",
  groundHeightLow: "货叉降至最低位离地高度(mm）",
  groundHeightHigh: "货叉上升最高位时车体高度（mm)",
  channelWidth: "巷道空间（1012托盘，单边200）(mm）",
  channelWidthSafe: "巷道空间安全距离（单边200）(mm)",
  turningRadius: "车体转弯半径（mm）",
  turningRadiusMin: "弧度转弯最小模拟半径（建议值，mm）",
  wheelMaterialId: "轮胎材质",
  wheelDrivingSize: "驱动轮尺寸（mm)",
  wheelLoadSize: "承载轮尺寸（mm)",
  wheelBalanceSize: "平衡轮尺寸（mm)",
  groundClearanceMin: "最小离地间隙（mm）",
  paPoCl: "爬坡能力（%）",
  wxCl: "外形尺寸（LxWx货叉最低位H，mm）",
  weight: "自重（kg）",
  driveModeId: "驱动方式",
  brakingMethodId: "制动方式",
  driveMotorPower: "驱动电机功率（kw）",
  xsCl: "最大行驶速度（m/s）",
  liftMotorPower: "提升电机功率（kw）",
  tsSdCl: "提升速度（m/s）",
  xjSdCl: "下降速度（m/s）",
  loadLineChartPath: "载荷曲线图"
};
const setInfo = (data: string | Array<any>) => {
  if (Array.isArray(data)) {
    data = data.length ? data : "";
  }
  return data || "--";
};
const transformItemData = (data: any) => {
  const str: string[] = [];
  data.content &&
    data.content.forEach((item: any) => {
      str.push(`标配--${item.sourceName}`);
    });
  data.contentOptional &&
    data.contentOptional.forEach((item: any) => {
      str.push(`选配--${item.sourceName}`);
    });
  return setInfo(str);
};
const transformData = (originData: any, initData: any, urlResource: string, getDictItem: any) => {
  // 处理头部数据
  originData.picture = urlResource + setInfo(originData["picture"]);
  const mapping: any = {
    standSizeCl:
      setInfo(originData["forkLength"]) + "×" + setInfo(originData["forkWidth"]) + "×" + setInfo(originData["forkThickness"]),
    paPoCl: setInfo(originData["climbEmpty"]) + "/" + setInfo(originData["climbFull"]),
    wxCl: setInfo(originData["length"]) + "×" + setInfo(originData["width"]) + "×" + setInfo(originData["height"]),
    xsCl: setInfo(originData["speedDriveEmpty"]) + "/" + setInfo(originData["speedDriveFull"]),
    tsSdCl: setInfo(originData["liftSpeedEmpty"]) + "/" + setInfo(originData["liftSpeedFull"]),
    xjSdCl: setInfo(originData["dropSpeedEmpty"]) + "/" + setInfo(originData["dropSpeedFull"]),
    loadLineChartPath: originData["loadLineChartPath"] ? urlResource + originData["loadLineChartPath"] : "",
    driveModeId: getDictItem("driveType", originData["driveModeId"]),
    brakingMethodId: getDictItem("brakingSystem", originData["brakingMethodId"]),
    wheelMaterialId: getDictItem("tireMaterial", originData["wheelMaterialId"])
  };
  // 主体信息处理
  const keys = Object.keys(mapping);
  for (const [key, value] of Object.entries(mainItemTitleList)) {
    if (keys.includes(key)) {
      initData[0].children.push({
        title: value,
        value: setInfo(mapping[key]),
        type: key === "loadLineChartPath" ? "image" : ""
      });
    } else {
      initData[0].children.push({
        title: value,
        value: setInfo(originData[key])
      });
    }
  }
  // 其余信息处理
  initData.forEach((item: any) => {
    if (item.id) {
      originData.propertyList.forEach((property: any) => {
        if (item.id === property.categoryId) {
          item.children.push({
            title: property.typeName,
            value: transformItemData(property)
          });
        }
      });
    }
  });
  return initData;
};

export const useDetail = (options: any) => {
  const urlResource: string | undefined = inject("urlResource");
  const { getDictAsync, getDictItem } = useDict(["driveType", "brakingSystem", "tireMaterial"]);

  const { id } = options;
  const detailData: any = ref({});
  const initData = [
    {
      name: "本体参数",
      id: "",
      children: []
    }
  ];
  const getOtherInfo = async () => {
    const { data } = await getDictAsync("agvConfig");
    data?.forEach((item: any) => {
      initData.push({
        name: item.name,
        id: item.id,
        children: []
      });
    });
  };
  const getDetail = async (id: number) => {
    await getOtherInfo();
    const { data } = await getProductModelDetail({ id });
    data.propertyList = transformData(data, initData, urlResource, getDictItem);
    detailData.value = data;
  };
  onMounted(() => {
    getDetail(id);
  });
  return {
    detailData
  };
};
